<template>
  <div style="height: 100vh;background-image: linear-gradient(135deg,#0eead8,#3f7a9b);overflow: hidden">
    <el-container style="padding: 30px">
      <el-header>
        <el-row :gutter="10" style="text-align: center">
          <el-col :span="24">
            <span style="color:#fff;font-size:30px;font-weight:bold;">口腔门诊系统</span>
          </el-col>
        </el-row>

      </el-header>

      <el-main>
        <div style="display: flex; justify-content: center;  height: 100vh;">
          <el-card style="margin-top: 80px;width: 500px;height: 350px">
            <h3 style="text-align: center">用户登录</h3>
            <el-form label-position="top">
              <el-form-item label="用户名">
                <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
              </el-form-item>

              <el-form-item label="密码">
                <el-input placeholder="请输入用密码" type="password" v-model="user.password"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="reg()" style="width: 30%;">注册</el-button>
                <el-button type="primary" @click="login()" style="width: 30%;">登录</el-button>
                <el-button type="primary" @click="doctorLogin()" style="width: 30%;position:absolute;right:20px;">医生登录
                </el-button>
              </el-form-item>
            </el-form>
            <div style="color: #666;text-align: center;font-size: 14px;padding: 10px 0">
              (体验账号:admin 密码:admin)
            </div>
          </el-card>
        </div>


      </el-main>

    </el-container>
  </div>
</template>

<script setup>
//1.创建用户对象
import {ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";

const user = ref({username: '', password: ''});

//注册方法
const reg = () => {
  let data = qs.stringify(user.value);
  console.log(data);
  axios.post('http://localhost:8080/v1/user/reg', data)
      .then(response => {
        if (response.data.code == 2000) {
          ElMessage.success('注册成功!');
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

//用户登录方法
const login = () => {
  let data = qs.stringify(user.value);
  console.log(data);
  axios.post('http://localhost:8080/v1/user/login', data)
      .then(response => {
        if (response.data.code == 2000) {
          ElMessage.success('登录成功!');
          localStorage.user = JSON.stringify(response.data.data);
          router.push('/user');
        } else {
          ElMessage.error(response.data.msg);
        }
      })
};

//医生登录
const doctorLogin = () => {
  let data = qs.stringify(user.value);
  console.log(data);
  axios.post('http://localhost:8080/v1/admin/login', data)
      .then(response => {
        if (response.data.code == 2000) {
          ElMessage.success('登录成功!');
          localStorage.user = JSON.stringify(response.data.data);
          router.push('/user');
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

</script>

<style scoped>

</style>